$(function () {
    obj.init();
});

var obj = new function () {
    return {
        form: null,
        clickFlag: false,
        List : [],
        init: function () {
            this.form = new xhForm($('#xh-form'));
            this.form.parse();
            this.funObj().addData();//获取select数据
            this.listener();
        },
        listener: function () {
            var $this = this;
            var clickFlag = this.clickFlag;
            var List = this.List;
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().saveData();
            });

            //滚动进度条 start
            var tag = false,ox = 0,left = 0,bgleft = 0;
            $('.progress_btn').mousedown(function(e) {
                ox = e.pageX - left;
                tag = true;
            });
            $(document).mouseup(function() {
                tag = false;
            });
            $('.progress').mousemove(function(e) {//鼠标移动
                if (tag) {
                    left = e.pageX - ox;
                    if (left <= 0) {
                        left = 0;
                    }else if (left > 120) {
                        left = 120;
                    }
                    $('.progress_btn').css('left', left);
                    $('.progress_bar').width(left);
                    $('.text').html(parseInt((left/120)*100) + '%');
                    $('#sProcess').val(parseInt((left/120)*100));
                }
            });
            $('.progress_bg').click(function(e) {//鼠标点击
                if (!tag) {
                    bgleft = $('.progress_bg').offset().left;
                    left = e.pageX - bgleft;
                    if (left <= 0) {
                        left = 0;
                    }else if (left > 120) {
                        left = 120;
                    }
                    $('.progress_btn').css('left', left);
                    $('.progress_bar').animate({width:left},120);
                    $('.text').html(parseInt((left/120)*100) + '%');
                    $('#sProcess').val(parseInt((left/120)*100));
                }
            });
            //滚动进度条 end

            var count = 0; //记录上传次数
            var fileInput = $("#fileInput");
            fileInput.on("fileuploaded", function (event, data, previewId, index) {
                count++;
                List.push({ FileName: data.response.msg.fileName, KeyID: previewId, url:data.response.msg.url });
                if(clickFlag && (count === data.files.length)){
                    clickFlag = false;
                    count = 0;
                    save();
                }
            });

            fileInput.on("filesuccessremove", function (event, data, previewId, index) {
                for (var i = 0; i < List.length; i++) {
                    if (List[i].KeyID == data) {
                        List.splice(i,1);
                    }
                }
            });
        },
        funObj: function () {
            var form = this.form;
            var $this = this;
            var type = $('#type').val();
            var iId = $('#iId').val();
            var school = $('#school').val();
            var progress = $('#sProcess').val();
            var teaArr = [];
            return {
                addData: function () {
                    $("[id=startTime]").datetimepicker({
                        format: 'yyyy-mm-dd',
                        language: 'zh-CN',
                        minView: "month",
                        autoclose: true
                    });
                    $("[id=endTime]").datetimepicker({
                        format: 'yyyy-mm-dd',
                        language: 'zh-CN',
                        minView: "month",
                        autoclose: true
                    });
                    if (type === '1'){//第一层
                        xhExtra.ajaxRequest('bpm/operate_pro/getConnTeaData', {id:iId,school:school}, {check:true}, function (data) {
                            teacherObj = $('#teaList').select2({placeholder: '选择教师',data: data.allTeaList,allowClear: true,multiple : true});
                            $.each(data.teaList, function (key, value) {
                                teaArr.push(value);
                            });
                            teacherObj.val(teaArr).trigger('change');//填充数据
                            teaArr = [];
                        });
                    }
                    var left = (progress/100)*120;
                    $('.progress_btn').css('left', left);
                    $('.progress_bar').width(left);
                    $('.text').html(parseInt((left/120)*100) + '%');
                    $('#iProcess').val(parseInt((left/120)*100));
                    $("#fileInput").fileinput('destroy').fileinput({
                        language: 'zh', //设置语言
                        uploadUrl: 'updateFile', //上传的地址
                        allowedFileExtensions : ['jpg', 'jpeg', 'png', 'gif', 'bpm', 'xlsx','docx', 'doc', 'pdf','xls'],//接收的文件后缀
                        uploadAsync: true, //默认异步上传
                        showUpload: true, //是否显示上传按钮
                        showCaption: false,//是否显示标题
                        dropZoneEnabled: false,
                        browseClass: "btn btn-primary", //按钮样式
                        maxFileSize: 50000,
                        maxFileCount: 10, //表示允许同时上传的最大文件个数
                        enctype: 'multipart/form-data',
                        validateInitialCount:true,
                        height:200,
                        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
                        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！"
                    });
                },
                saveData: function () {
                    var startTime = $('#startTime').val();
                    var endTime = $('#endTime').val();
                    var remark = $('#remark').val();
                    var sProcess = $('#sProcess').val();
                    var params = {};
                    var List = $this.List;
                    var FileUrl = '';
                    if (JSON.stringify(List) !== '[]'){
                        for (var i = 0; i < List.length; i++) {
                            FileUrl += List[i]['url']+',';
                        }
                    }
                    if (type === '1'){//第一层
                        var teaList = $('#teaList').val();
                        params = {type:type, id:iId, startTime:startTime, endTime:endTime, remark:remark, sProcess:sProcess, fileUrl:FileUrl, teaList:teaList};
                    } else {
                        params = {type:type, id:iId, startTime:startTime, endTime:endTime, remark:remark, sProcess:sProcess, fileUrl:FileUrl};
                    }
                    xhExtra.ajaxRequest('bpm/operate_pro/modifyOperateProData', params, {success: true,check:false},function (res) {
                        if(res.code === 200){
                            window.top.xhTop.showSuccess(res.msg);
                            window.top.xhTop.getActiveFrame().obj.table.treegrid('reload', {openId: iId});
                            if (iId.indexOf("_") === -1 ) {
                                window.top.xhTop.getActiveFrame().obj.table.treegrid('reload', {openId: iId});
                            }
                            window.top.xhTop.closeModal();
                        } else {
                            window.top.xhTop.showError(res.error);
                            xhExtra.setBtnNormal($('#bt_modal_confirm',window.top.document));
                        }
                    });
                }
            }
        }
    }
};
